﻿using UnityEngine;
using System.Collections;

public class HUDManager : MonoBehaviour
{
	[HideInInspector]
	public static HUDManager instance;

	[HideInInspector]
	public float finishTime;

	public GUIText timer;
	public GUIText counter;

	private float tTimer;
	private float tMinutes;
	private float tSeconds;
	private float tMiliseconds;
	private float countDown;

	void Awake()
	{
		instance = this;
	}
	
	void Start()
	{
		tMinutes = 0;
		tSeconds = 0;
		tMiliseconds = 0;

		countDown = 4f;
		PlayerController.instance.moving = false;
	}

	public float GetTime()
	{
		PlayerController.instance.moving = false;
		finishTime = ((tMinutes * 60) + tSeconds + tMiliseconds);
		return finishTime;
	}

	void Update()
	{
		if (countDown > 0)
		{
			counter.text = ((int)countDown).ToString();
			timer.text = "";
			countDown -= Time.deltaTime;
		}
		else if (countDown < 1)
		{
			PlayerController.instance.moving = true;
			counter.text = "";

			tMiliseconds += Time.deltaTime;
			
			if(tMiliseconds >= 1.0f)
			{
				tSeconds++;
				tMiliseconds = 0;
			}
			if(tSeconds >= 60.0f)
			{
				tMinutes++;
				tSeconds = 0;
			}

			float renderMiliseconds = tMiliseconds * 100f;

			if (PlayerController.instance.moving)
			{
				timer.text = tMinutes.ToString() + ":" + tSeconds.ToString() + ":" + ((int)renderMiliseconds).ToString();
				counter.text = "";
			}
			else
			{
				timer.text = finishTime.ToString();
				Application.LoadLevel ("Level_Menu_Button");
			}
		}
	}
}